#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# -*- mode: python -*-

from __future__ import print_function
from __future__ import absolute_import
from __future__ import division

def map_class_name(name):
    if name == u'Exception':
        return u'CompilationException'
    else:
        return name + u'Exception'

def generate_class_code(subcls, cls, fout):
    print(u'    [Serializable]', file=fout)
    print(u'    public class %s : %s' % (subcls, cls), file=fout)
    print(u'    {', file=fout)
    print(u'        public %s()' % subcls, file=fout)
    print(u'        {', file=fout)
    print(u'        }', file=fout)
    print(file=fout)
    print(u'        public %s(string message)' % subcls, file=fout)
    print(u'            : base(message)', file=fout)
    print(u'        {', file=fout)
    print(u'        }', file=fout)
    print(file=fout)
    print(u'        public %s(string message, Exception inner)' % subcls, file=fout)
    print(u'            : base(message, inner)', file=fout)
    print(u'        {', file=fout)
    print(u'        }', file=fout)
    print(file=fout)
    print(u'        protected %s(SerializationInfo info, StreamingContext context)' % subcls, file=fout)
    print(u'            : base(info, context)', file=fout)
    print(u'        {', file=fout)
    print(u'        }', file=fout)
    print(u'    }', file=fout)

def main():
    import io
    import os
    dir_path = os.path.dirname(__file__)
    input_path = os.path.join(dir_path, 'compilation_exceptions.txt')
    output_path = os.path.join(dir_path, 'CommonExceptions.cs')
    with io.open(input_path, encoding='utf-8-sig') as fin:
        with io.open(output_path, 'w', encoding='utf-8-sig', newline='\r\n') as fout:
            print(u'using System;', file=fout)
            print(u'using System.Runtime.Serialization;', file=fout)
            print(file=fout)
            print(u'namespace ParserGenerator.Exceptions', file=fout)
            print(u'{', file=fout)
            for i, line in enumerate(fin):
                if i > 0:
                    print(file=fout)
                subcls, cls = line.strip().split(u': ')
                subcls = map_class_name(subcls)
                cls = map_class_name(cls)
                generate_class_code(subcls, cls, fout)
            print(u'}', file=fout)

if __name__ == '__main__':
    main()
